import { getAction, postAction } from '@/api/manage'
import { UtilsMixin } from '@/mixins/UtilsMixin'
import { TableMixin } from '@/mixins/TableMixin'
import { CodesMixin } from '@/mixins/CodesMixin'
import { formatDate, parseTime } from '@/utils/util'
export const ProjectChangeListMixin = {
  mixins: [UtilsMixin, TableMixin, CodesMixin],
  data() {
    return {
      currUserId: this.$store.getters.userInfo.username,
      urls: {
        list: '/projectChange/page',
        delete: '/projectChange/delete',
      },
      keywordType: '1',
      records: [],
      columns: [
        {
          title: '序号',
          dataIndex: 'sortIndex',
          width: '80px',
          align: 'center',
          customRender: (text, record, index) => {
            return index + 1
          },
        },
        {
          title: '审批状态',
          dataIndex: 'status',
          scopedSlots: { customRender: 'statusRender' },
          width: '100px',
          align: 'center',
          customCell: () => {
            return this.customCellRender(130)
          },
          customRender: (text, record) => {
            return this.tooltipRender(this.getCodeNames(text, '工程项目变更状态'), record)
          },
        },
        {
          title: '变更单号',
          dataIndex: 'projectChangeNo',
          align: 'center',
          width: '130px',
          customCell: () => {
            return this.customCellRender(130)
          },
          customRender: (text, record) => {
            return this.tooltipActiveRender(text, record, 'toDetail')
          },
        },
        {
          title: '项目编号',
          dataIndex: 'projectNo',
          align: 'center',
          width: '130px',
          customCell: () => {
            return this.customCellRender(130)
          },
          customRender: (text, record) => {
            return this.tooltipActiveRender(text, record, 'toProjectDetail')
          },
        },
        {
          title: '项目名称',
          dataIndex: 'projectName',
          align: 'left',
          width: '230px',
          customCell: () => {
            return this.customCellRender(230)
          },
          customRender: (text, record) => {
            return this.tooltipRender(text, record)
          },
        },
        {
          title: '变更名称',
          dataIndex: 'projectChangeName',
          align: 'left',
          width: '230px',
          customCell: () => {
            return this.customCellRender(230)
          },
          customRender: (text, record) => {
            return this.tooltipRender(text, record)
          },
        },
        {
          title: '项目经理',
          dataIndex: 'projectManager',
          align: 'center',
          width: '90px',
          customCell: () => {
            return this.customCellRender(90)
          },
          customRender: (text, record) => {
            return this.tooltipRender(text, record)
          },
        },
        {
          title: '责任部门',
          dataIndex: 'dutyDeptName',
          align: 'center',
          width: '100px',
          customCell: () => {
            return this.customCellRender(100)
          },
          customRender: (text, record) => {
            return this.tooltipRender(text, record)
          },
        },
        {
          title: '主要责任人',
          dataIndex: 'dutyUserName',
          align: 'center',
          width: '110px',
          customCell: () => {
            return this.customCellRender(110)
          },
          customRender: (text, record) => {
            return this.tooltipRender(text, record)
          },
        },
        {
          title: '变更类型',
          dataIndex: 'changeType',
          align: 'center',
          width: '100px',
          // customCell: () => {
          //   return this.customCellRender(100)
          // },
          // customRender: (text, record) => {
          //   return this.tooltipRender(this.getCodeNames(text, '工程项目变更类型'), record)
          // },
        },
        {
          title: '预算变更内容',
          dataIndex: 'budgetChangeContent',
          align: 'center',
          width: '230px',
          customCell: () => {
            return this.customCellRender(230)
          },
          customRender: (text, record) => {
            return this.tooltipRender(this.getCodeNames(text, '工程项目项目预算变更类型'), record)
          },
        },
        {
          title: '原总预算费用',
          dataIndex: 'originAmount',
          align: 'center',
          width: '130px',
        },
        {
          title: '历史变更费用',
          dataIndex: 'historyChangeAmount',
          align: 'center',
          width: '130px',
        },
        {
          title: '本次变更费用',
          dataIndex: 'changeAmount',
          align: 'center',
          width: '130px',
        },
        {
          title: '追加SAP费用',
          dataIndex: 'addToSapTotal',
          align: 'center',
          width: '130px',
        },
        {
          title: '变更费用占比',
          dataIndex: 'ChangePercent',
          align: 'center',
          width: '130px',
        },
        {
          title: '其他变更内容',
          dataIndex: 'changeContent',
          align: 'center',
          width: '230px',
          customCell: () => {
            return this.customCellRender(230)
          },
          customRender: (text, record) => {
            let text1 = this.getCodeNames(text, '工程项目其他变更内容')
            let text2 = this.getCodeNames(record.contractChangeContent, '工程项目变更合同涉及内容')

            if (text1 && text2) {
              return this.tooltipRender(text1 + ',' + text2, record)
            } else if (text1) {
              return this.tooltipRender(text1, record)
            } else if (text2) {
              return this.tooltipRender(text2, record)
            }
          },
        },
        {
          title: '变更原因说明',
          dataIndex: 'changeReason',
          align: 'center',
          width: '230px',
          customCell: () => {
            return this.customCellRender(230)
          },
          customRender: (text, record) => {
            return this.tooltipRender(text, record)
          },
        },
        {
          title: '申请人',
          dataIndex: 'createUserName',
          align: 'center',
          width: '80px',
        },
        {
          title: '申请日期',
          dataIndex: 'createTime',
          align: 'center',
          width: '100px',
          customCell: () => {
            return this.customCellRender(100)
          },
          customRender: (text, record) => {
            return this.timeRender(text, record, 'yyyy-MM-dd')
          },
        },
      ],
      showSearchForm: false,
      loading: false,
    }
  },
  async mounted() {
    let types = []
    types.push('工程项目变更状态')
    types.push('工程项目变更类型')
    types.push('工程项目项目预算变更类型')
    types.push('工程项目其他变更内容')
    types.push('工程项目变更合同涉及内容')
    await this.fetchCodes(types)
  },
  methods: {
    changeKeywordType() {
      if (this.keywordType == '1') {
        this.$set(this.searchForm, 'projectChangeNo', '')
      } else {
        this.$set(this.searchForm, 'projectNo', '')
      }
    },
    handleToSearch() {
      this.searchForm.pageNo = 1
      this.showSearchForm = false
      this.fetchList()
    },
    fetchList() {
      this.loading = true
      this.records = []
      this.$set(this.searchForm, 'withInProjectClass', '40,41')
      this.$set(this.searchForm, 'withOutProjectTypeSubCategory', 'S')
      postAction(this.urls.list, this.searchForm).then((res) => {
        if (res.success) {
          this.records = res.result.records
          this.$set(this.searchForm, 'total', res.result.total)
        } else {
          this.$message.error('查询工程项目变更数据错误:' + res.message)
        }
        this.loading = false
      })
    },
    handleAdd() {
      let urlJump = this.$router.resolve({
        path: '/projectChange/changeRecord',
      })
      window.open(urlJump.href, '_blank')
    },
    handleEdit() {
      if (!this.selectedRows || this.selectedRows.length <= 0) {
        return this.$message.warning('请选择一条记录操作')
      }
      let urlJump = this.$router.resolve({
        path:
          '/projectChange/changeRecord' +
          '?changeId=' +
          this.selectedRows[0].id +
          '&projectBuildId=' +
          this.selectedRows[0].projectBuildId +
          '&pageId=8',
      })
      window.open(urlJump.href, '_blank')
    },
    toDetail(record) {
      let urlJump = this.$router.resolve({
        path:
          '/projectChange/changeRecord' +
          '?changeId=' +
          record.id +
          '&projectBuildId=' +
          record.projectBuildId +
          '&pageId=8',
      })
      window.open(urlJump.href, '_blank')
    },

    toProjectDetail(record) {
      window.open(
        this.HTE_OLD_SERVER_URL + `/projectBuild/auditDetail?bootToken=${Vue.ls.get(ACCESS_TOKEN)}&id=${record.id}`,
        '_blank'
      )
    },
    handleDel() {
      if (!this.selectedRows || this.selectedRows.length <= 0) {
        return this.$message.warning('请选择一条记录操作')
      }
      let projectChangeNos = []
      for (let r of this.selectedRows) {
        projectChangeNos.push(r.projectChangeNo)
        if (r.status == '2') {
          return this.$message.warning('请勿删除审批通过的工程项目变更记录')
        }
        if ([2, 4].indexOf(r.status) >-1){
          if(r.createUserId != this.currUserId) {
            return this.$message.warning('审批中或审批不通过只能由流程发起人执行删除')
          }
        } 
      }
      let that = this
      this.$confirm({
        content: `是否确定删除工程项目变更记录[${projectChangeNos.join(',')}]？`,
        onOk: () => {
          getAction(that.urls.delete + '?id=' + that.selectedRowKeys.join(',')).then((res) => {
            if (res.success) {
              that.selectedRows = []
              that.selectedRowKeys = []
              that.$message.success('删除成功')
              that.fetchList()
            } else {
              that.$message.error('删除失败:' + res.message)
            }
          })
        },
      })
    },
    handleReload() {
      this.searchForm.total = 0
      this.fetchList()
    },
    handleExport() {
      this.searchForm.selectedIds = this.selectedRowKeys.join(',')
      let url = process.env.VUE_APP_API_BASE_URL + '/projectChange/export?1=1'
      this.handleExportXlsByPost(parseTime(new Date(), '{y}{m}{d}') + '《工程项目变更列表》', url, this.searchForm)
    },
    currentHandlerRenderFunc(record) {
      let text = ''
      if (record.handlerUser) {
        if (record.handlerTime) {
          text = record.handlerUser + '/' + record.handlerTime + '/' + record.stayDays + '天'
        }
      }
      return text
    },
  },
}
